package com.waymap.service.impl;

import com.waymap.Thread.WmsThread;
import com.waymap.common.vo.ItemInventoryInfoVo;
import com.waymap.domain.InventorysAdjust;
import com.waymap.domain.ItemInventorys;
import com.waymap.domain.TransactionItems;
import com.waymap.domain.Transactions;
import com.waymap.mapper.*;
import com.waymap.service.ItemInventoryService;
import com.waymap.utils.NewNo;
import com.waymap.utils.TrainIdWorker;
import com.waymap.utils.mq.MsgProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ItemInventoryServiceImpl extends BaseService<ItemInventorys> implements ItemInventoryService {

    @Resource
    private ItemInventorysMapper itemInventorysMapper;
    @Autowired
    private MsgProducer producer;
    @Resource
    private TransactionItemsMapper transactionItemsMapper;
    @Resource
    private TransactionItemInventoryDetailsMapper transactionItemInventoryDetailMapper;
    @Resource
    private TransactionsMapper transactionsMapper;
    @Resource
    private NewNo newNo;
    /**
     * @param param
     * @return
     * @author vincent mai
     * 移库-查询商品信息
     */
    @Override
    public List<Map<String, Object>> getItemInfoList(Map<String, Object> param) {
        /*Map<String, Object> paramVo = new HashMap(16);
        List<ItemInventoryInfoVo> list = new ArrayList<>();
        List<ItemInventoryInfoVo> itemInventorysList = itemInventorysMapper.getItemInventoryInfoList(param);
        if (null != itemInventorysList && itemInventorysList.size() > 0) {
            for (ItemInventoryInfoVo vo : itemInventorysList) {
                ItemInventoryInfoVo itVo = new ItemInventoryInfoVo();
                itVo.setItemDescription(vo.getItemDescription());
                itVo.setItemName(vo.getItemName());
                itVo.setItemId(vo.getItemId());
                itVo.setLocationId(vo.getLocationId());
                itVo.setUpc(vo.getUpc());
                paramVo.put("locationId", vo.getLocationId());
                List<Map<String, Object>> locationList = itemInventorysMapper.getBinNameList(paramVo);//根据locationId查询对应的仓库位置
                if (null != locationList && locationList.size() > 0) {
                    itVo.setLocationList(locationList);
                }
                list.add(itVo);
            }
        }*/
        List<Map<String, Object>> itemInventoryInfoList = itemInventorysMapper.getItemInfoList(param);
        return itemInventoryInfoList;
    }

    /**
     * @author vincent mai
     */
    @Override
    public List<Map<String, Object>> getBinNameList2(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return itemInventorysMapper.getBinNameList2(param);
    }

    @Override
    public List<Map<String, Object>> selectByItemId(Map<String, Object> param) {
        return itemInventorysMapper.selectByItemId(param);
    }

    @Override
    public List<Map<String, Object>> getLocationBinsList(Map<String, Object> param) {
        return itemInventorysMapper.getLocationBinsList(param);
    }

    @Override
    public int InventorysAdjust(InventorysAdjust inventorysAdjust) {
        /**
         * 生成 trainID
         */
        String trainId = "ADJUST"+TrainIdWorker.getNewId();
        Transactions tranVo = new Transactions();
        tranVo.setTransactionType("INVENTORY_ADJUST");
        tranVo.setCreatedBy(inventorysAdjust.getCreateByUser());
        tranVo.setLastUpdateBy("SYNC");
        tranVo.setSyncToNs("N");
        tranVo.setErrorReason("null");
        tranVo.setNum(0);
        tranVo.setLockedByUser("0");
        tranVo.setStatus("COMPLETE");
        tranVo.setAccountId(inventorysAdjust.getAccountId());
        tranVo.setLocationId(inventorysAdjust.getLocationId());
        tranVo.setTranid(trainId);
        transactionsMapper.insert(tranVo);

        TransactionItems ti = new TransactionItems();
        ti.setAccountId(inventorysAdjust.getAccountId());
        ti.setItemId(inventorysAdjust.getItemId());
        ti.setBinId(inventorysAdjust.getBinId());
        ti.setTransactionId(trainId);
        ti.setQuantity(inventorysAdjust.getAdjustNum());
        if(inventorysAdjust.getStockType()==1){
            //入库操作
            ti.setLotNumber(newNo.getNewNo());
            ti.setQuantityReceived(inventorysAdjust.getAdjustNum());
            ti.setQuantityFulfilled(0);
        }else {
            //出库
            ti.setLotNumber(inventorysAdjust.getLotNumber());
            ti.setQuantityFulfilled(inventorysAdjust.getAdjustNum());
            ti.setQuantityReceived(0);
        }
        ti.setQuantityScaned(0);
        int result = transactionItemsMapper.insert(ti);
       if(result>0){
           List<TransactionItems> select = transactionItemsMapper.select(ti);
           if(select.size()>0){
               TransactionItems transactionItems = select.get(0);
               /*****利用线程发送ACTIVE_MQ模块*****/
               WmsThread wms = new WmsThread("wms-mq", trainId, "INVENTORY_ADJUST", producer, "INVENTORY_ADJUST", transactionItems.getAccountId());
               wms.start();
           }
       }
        return 1;
    }
}
